/*
 * OcTerminartHome.java
 *
 * author: Daniel Rudigier
 * date: 20.05.2008
 * project: ocumed
 * 
 * Classes:
 *  OcTerminartHome
 *
 * Generated using:
 *  Hibernate Tools 3.2.1.GA, 20.05.2008 11:24:37
 *  and a powerfull PHP script, using PHP Version 5.2.0
 *
 */

package ocumed.persistenz.hibernate;

import java.util.List;

import ocumed.persistenz.dao.TerminartDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;  

/**
 * Home object for domain model class OcTerminart.
 * @see ocumed.persistenz.hibernate.OcTerminart
 * @author Hibernate Tools
 */
public class OcTerminartHome implements TerminartDAO {

    private static final Log log = LogFactory.getLog(OcTerminartHome.class);

    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public void attachDirty(OcTerminart instance) {
        log.debug("attaching dirty OcTerminart instance");
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public void attachClean(OcTerminart instance) {
        log.debug("attaching clean OcTerminart instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public void delete(OcTerminart persistentInstance) {
        log.debug("deleting OcTerminart instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public OcTerminart merge(OcTerminart detachedInstance) {
        log.debug("merging OcTerminart instance");
        try {
            OcTerminart result = (OcTerminart) sessionFactory
                    .getCurrentSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public OcTerminart findById(int id) {
        log.debug("getting OcTerminart instance with id: " + id);
        try {
            OcTerminart instance = (OcTerminart) sessionFactory
                    .getCurrentSession().get(
                            "ocumed.persistenz.hibernate.OcTerminart", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    /** 
     * commit a transaction
     */
    public void commit() {
        sessionFactory.getCurrentSession().getTransaction().commit();
    }

    /**
     * rollback a transaction
     */
    public void rollback() {
        sessionFactory.getCurrentSession().getTransaction().rollback();
    }

    /** 
     * start a transaction
     */
    public void start() {
        sessionFactory.getCurrentSession().getTransaction().begin();
    }  

    /**
     * find all OcTerminart instances
     */
    @SuppressWarnings("unchecked")
    public List<OcTerminart> findAll() {
        log.debug("finding all OcTerminart instances ");
        try {
            List<OcTerminart> results = sessionFactory
                    .getCurrentSession().createCriteria(OcTerminart.class).list();
            log.debug("finding all OcTerminart successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("finding all OcTerminart failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    /**
     * persist a object, and return the generated id
     *
     * @param transientInstance
     * @return generated id 
     */
    public Integer persist(OcTerminart transientInstance) {
        log.debug("persisting OcTerminart instance");
        try {            
            Integer ret = (Integer) sessionFactory.getCurrentSession().save(transientInstance); 
            log.debug("persist successful");
            return ret;
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }
}
